package com.ruoyi.system.interAttr.attr;

import com.alibaba.fastjson.JSONArray;
import com.alibaba.fastjson.JSONObject;
import com.ruoyi.common.core.redis.RedisCache;
import com.ruoyi.common.utils.DateUtils;
import com.ruoyi.common.utils.UUIDGenerator;
import com.ruoyi.common.utils.http.HttpUtils;
import com.ruoyi.system.V2.gameAttr.mapper.GameAttributeMapper;
import com.ruoyi.system.interAttr.domain.GamesRes;
import com.ruoyi.system.interAttr.domain.InterAttrRes;
import com.ruoyi.system.interAttr.domain.InterServerRes;
import lombok.extern.slf4j.Slf4j;
import org.apache.commons.codec.digest.DigestUtils;
import org.apache.commons.collections4.CollectionUtils;
import org.springframework.stereotype.Service;

import javax.annotation.Resource;
import java.util.ArrayList;
import java.util.List;
import java.util.Map;
import java.util.Objects;
import java.util.concurrent.TimeUnit;
import java.util.stream.Collectors;

@Slf4j
@Service
public class KeJinShouAttr {
    @Resource
    GameAttributeMapper gameAttributeMapper;
    @Resource
    private RedisCache redisCache;


    private static String SERVICE_NAME = "KJS";


    public static void main(String[] args) {
//         getGames();//获取全部游戏
//        getServer(211); //获取区服
//        getPropety(211); // 获取商品属性
    }

    public List<GamesRes> getGames() {
        String url = "https://kejinlianmeng.com/api_up/account/config/games";
        Map params = getCove();
        String paramsStr = JSONObject.toJSONString(params);
        String response = HttpUtils.sendPost(url, paramsStr, "application/json");
//        System.out.println(response);
        JSONObject resJson = JSONObject.parseObject(response);
        List<JSONObject> jsonObjects = JSONObject.parseArray(resJson.getString("data"), JSONObject.class);

        List<GamesRes> gamesResList = new ArrayList<>();

        for (JSONObject jsonObject : jsonObjects) {
            GamesRes gamesRes = new GamesRes();
            String id = jsonObject.getString("id");
            String title = jsonObject.getString("title");
            gamesRes.setId(id);
            gamesRes.setGameName(title);
            gamesResList.add(gamesRes);
        }
        return gamesResList;
    }

    public  List<InterServerRes> getServer(String gameId) {
        String url = "https://kejinlianmeng.com/api_up/account/config/servers";
        Map params = getCove();
        params.put("game_id", gameId);
        String paramsStr = JSONObject.toJSONString(params);
        String response = HttpUtils.sendPost(url, paramsStr, "application/json");
//        System.out.println(response);
        JSONObject jsonObject = JSONObject.parseObject(response);
        List<JSONObject> data = JSONObject.parseArray(jsonObject.getString("data"), JSONObject.class);

        List<InterServerRes> interServerRes = new ArrayList<>();
        for (JSONObject datum : data) {
            InterServerRes interServer = new InterServerRes();
            String id = datum.getString("id");
            interServer.setId(id);
            String title = datum.getString("title");
            interServer.setName(title);
            String servers = datum.getString("servers");
            List<InterServerRes> interServerRes1 = new ArrayList<>();
            List<JSONObject> jsonObjects = JSONObject.parseArray(servers, JSONObject.class);
            for (JSONObject object : jsonObjects) {
                InterServerRes interServer1 = new InterServerRes();
                String id1 = object.getString("id");
                String title1 = object.getString("title");
                interServer1.setId(id1);
                interServer1.setName(title1);
                interServerRes1.add(interServer1);
            }
            interServer.setInterServerRes(interServerRes1);
            interServerRes.add(interServer);
        }
        return interServerRes;
    }

    public List<InterAttrRes> getPropety(Long gameId, String type) {
        String key = SERVICE_NAME+"_"+gameId+"_"+type;
      try{
          List<InterAttrRes> resultList = redisCache.getCacheObject(key);
          if (CollectionUtils.isNotEmpty(resultList)){
              return resultList;
          }
      }catch ( Exception e){
         log.error("redis 读取失败");
      }

        String url = "https://kejinlianmeng.com/api_up/account/config/property";
        Map params = getCove();
        params.put("game_id", gameId);
        String paramsStr = JSONObject.toJSONString(params);
        String response = HttpUtils.sendPost(url, paramsStr, "application/json");
//        System.out.println(response);
        Integer status = JSONObject.parseObject(response).getInteger("status");
        List<InterAttrRes> interAttrResList = new ArrayList<>();
        if (0 == status) {
            JSONObject jsonObject = JSONObject.parseObject(response);
            List<JSONObject> data = JSONArray.parseArray(jsonObject.getString("data"), JSONObject.class);
            List<JSONObject> collect = new ArrayList<>();
            if (Objects.equals("1",type)){

                 collect = data.stream().filter(d -> d.getString("type").equals("radio")).collect(Collectors.toList());
            }else if  (Objects.equals("2",type)){
                 collect = data.stream().filter(d -> d.getString("type").equals("checkbox")).collect(Collectors.toList());
            }else if (Objects.equals("3",type)){
                collect = data.stream().filter(d -> d.getString("type").equals("input")).collect(Collectors.toList());
            }
            for (JSONObject object : collect) {
                InterAttrRes interAttrRes = new InterAttrRes();
                String id = object.getString("id");
                String title = object.getString("title");
                String val = object.getString("val");
                String required = object.getString("is_required");
                interAttrRes.setUuid(UUIDGenerator.generateUuid6());
                interAttrRes.setId(id);
                interAttrRes.setName(title);
                if (Objects.equals("Y",required)){
                    interAttrRes.setRequired(true);
                }else {
                    interAttrRes.setRequired(false);
                }
                List<InterAttrRes> interAttrResList1 = new ArrayList<>();
                List<JSONObject> jsonObjects = JSONObject.parseArray(val, JSONObject.class);
                for (JSONObject jsonObject1 : jsonObjects) {
                    InterAttrRes interAttrRes1 = new InterAttrRes();
                    String id1 = jsonObject1.getString("id");
                    interAttrRes1.setId(id1);
                    interAttrRes1.setUuid(UUIDGenerator.generateUuid6());
                    String title1 = jsonObject1.getString("title");
                    interAttrRes1.setName(title1);
                    interAttrResList1.add(interAttrRes1);
                }
                interAttrRes.setInterServerRes(interAttrResList1);
                interAttrResList.add(interAttrRes);
            }
        }
        try{
            redisCache.setCacheObject(key, interAttrResList, 3, TimeUnit.MINUTES);
        }catch (Exception e){
            log.error("redis 存入失败");
        }
        return interAttrResList;
    }

    static Map getCove() {
        String key = "878kjs36bfd324d8";
        String secret = "dd989607c0c5b35422a4f69d306bb0";

        JSONObject params = new JSONObject();
        params.put("key", key);
        params.put("secret", secret);
        String uuid4 = UUIDGenerator.generateUuid6();
        params.put("rand_str", uuid4);
        long time = DateUtils.getNowDate().getTime() / 1000;
        params.put("timestamp", time);
        String oldSign = "key=" + key + ",secret=" + secret + ",rand_str=" + uuid4 + ",timestamp=" + time;
        String sign = DigestUtils.md5Hex(oldSign);
        sign = DigestUtils.sha1Hex(sign + uuid4);
        params.put("sign", sign);
        return params;
    }


}